<?php
/**
 * 文章分类
 */
include LIB_PATH . '/YAction.class.php';
class ArticleAction extends YAction {
	public function __construct() {
		parent::__construct();

		// 登录检测
		if(!$this->isLogin()) {
			echo '_error Access Timeout'; exit;
		}
	}
	
	public function index() {
		// 列表
		$data = D('Article')->getListByPage();
		$pageString = $data['pageString'];
		unset($data['pageString']);
		$this->assign('data', $data);
		$this->assign('pageString', $pageString);
		
		$this->display();
	}
	
	// 添加
	public function addArticle() {
		$types = D('Articletype')->getByCondition('1=1');
		$this->assign('types', $types);
		
		$this->display();
	}
	public function doAddArticle() {
		$data = array();
		
		$data['articletypeid'] = y_getParam('articletypeid');
		$data['isrecommend'] = y_getParam('isrecommend');
		$data['title'] = y_getParam('title');
		$data['author'] = y_getParam('author');
		$data['tags'] = y_getParam('tags');
		$data['viewimage'] = y_getParam('viewimage');
		$data['description'] = y_getParam('description');
		$data['content'] = y_stripslashes($_POST['content']);
		
		$data['posttime'] = time();
		
		if(empty($data['title']) /*|| empty($data['content'])*/) {
			$this->error('数据不完整');
			exit;
		}
		
		$effectRows = D('Article')->addOne($data);
		
		$this->success('添加成功');
	}
	
	// 修改
	public function editArticle() {
		$id = y_getParam('id', 'get');
		if(!is_numeric($id)) {exit;}
		
		$types = D('Articletype')->getByCondition('1=1');
		$this->assign('types', $types);
		
		$data = D('Article')->getOneByPk($id);
		$this->assign('data', $data);
		
		$this->display();
	}
	public function doEditArticle() {
		$data = array();
		$id = y_getParam('pkid');
		if(!is_numeric($id)) {exit;}
		
		$data['articletypeid'] = y_getParam('articletypeid');
		$data['isrecommend'] = y_getParam('isrecommend');
		$data['title'] = y_getParam('title');
		$data['author'] = y_getParam('author');
		$data['tags'] = y_getParam('tags');
		$data['description'] = y_getParam('description');
		$data['content'] = y_stripslashes($_POST['content']);
		$data['posttime'] = time();
		
		$image = y_getParam('viewimage');
		if(!empty($image)) {$data['viewimage'] = $image;}
		
		if(empty($data['title']) /*|| empty($data['content'])*/) {
			$this->error('数据不完整');
			exit;
		}
		
		D('Article')->updateOne('id='.$id, $data);
		
		$this->success('修改成功');
	}
	
	// 删除
	public function delArticle() {
		$id = y_getParam('id', 'get');
		if(!is_numeric($id)) {exit;}
		
		D('Article')->deleteOne('id='.$id);
		
		$this->success('删除成功');
	}
}
